#include "MainController.h"

using namespace std;

MainController::MainController()
{
	pointsThread = new LoadingPointsThread();

	connect(pointsThread, SIGNAL(workResult(const QPolygonF)), this, SLOT(processThreadData(const QPolygonF)));
}

MainController::~MainController()
{
}

void MainController::SetModel(MainModel* model)
{
	_model = model;

}
void MainController::plotSaved(const QString &fileName, const QString &filter)
{
	/// TODO : tu bedzie mozna wyswietlic na dole informacje, ze plik zostal zapisany.
	emit savePlot(fileName,filter);
}

void MainController::handleFileLoading(const QString &fileName) 
{	
	QStringList qsl = fileName.split("/");

	_model->setLoadedFileName(qsl.at(qsl.length()-1).toUtf8().constData());

	pointsThread->setFileName(fileName);
	pointsThread->start();
}

void MainController::closeFile()
{
	QTimer::singleShot(3500, this, SLOT(countTime()));

	_model->setLoadedFileName("File has been closed");

	emit resetView();
}

void MainController::countTime()
{
	_model->setLoadedFileName("");
	
	emit resetLoadedFileName();
}

void MainController::displayStatusBarMessage(const int pointsSize)
{
	switch(pointsSize)
	{
	case NO_MESSAGE : _model->setStatusBarMessage(QString()); break;

	case 1 : _model->setStatusBarMessage(QString("Choose right background")); break;
	case MAX : _model->setStatusBarMessage(QString("Choose maximum")); break;

	case NR_OF_PEAKS : _model->setStatusBarMessage(QString("Insert number of peaks")); break;
	case CONFIRM : _model->setStatusBarMessage(QString("Click enter to confirm number of peaks")); break;
	}

	emit updateStatusBar();
}

void MainController::processThreadData(const QPolygonF curvePoints)
{
	emit updateView();
	
	emit parsingFileFinished(curvePoints);

	/// wyswietlam informacje o tym, ze nalezy wczytac pliki
	displayStatusBarMessage(NR_OF_PEAKS);
}